package com.example.shareapp.utils;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.TextUtils;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * Author:    hup
 * Date:      2016/8/10.
 * Description:
 */
public class FileUtil {


    private static final String PRE_FILE_NAME_KIT = "kit";
    /** 登录名 **/
    public static final String PRE_FILE_OPENID = "openID";
    public static final String PRE_FILE_DEVICEID = "DeviceID";
    public static final String USER_PHONE_NUM = "phoneNum";
    public static final String USER_PHONE_PWD = "user_pwd";
    public static final String USER_PHONE_NAME = "user_name";
    public static final String USER_PHONE_SEX = "user_sex";
    public static final String USER_PHONE_TOKEN = "token";

    private static SharedPreferences mKITPreferences;


    /**
     * 获取KITsharepreference
     *
     * @param context
     * @return
     */
    public static SharedPreferences getKITSharedPreferences(Context context) {
        if (mKITPreferences == null && context != null) {
            mKITPreferences = context.getSharedPreferences(PRE_FILE_NAME_KIT, Context.MODE_MULTI_PROCESS
                    + Context.MODE_PRIVATE );
        }
        return mKITPreferences;
    }

    public static void saveString(Context context, String key, String value) {
        SharedPreferences pre = getKITSharedPreferences(context);
        pre.edit().putString(key, value).commit();
    }

    public static String getString(Context context, String key) {
        SharedPreferences pre = getKITSharedPreferences(context);
        return pre.getString(key, "");
    }

    public static void saveLong(Context context, String key, long value) {
        SharedPreferences pre = getKITSharedPreferences(context);
        pre.edit().putLong(key, value).commit();
    }
    public static void saveBoolean(Context context, String key, boolean value) {
        SharedPreferences pre = getKITSharedPreferences(context);
        pre.edit().putBoolean(key, value).commit();
    }
    public static boolean getBoolean(Context context, String key) {
        SharedPreferences pre = getKITSharedPreferences(context);
        return pre.getBoolean(key, false);
    }

    public static long getLong(Context context, String key) {
        SharedPreferences pre = getKITSharedPreferences(context);
        return pre.getLong(key, 0);
    }

    public static void saveInt(Context context, String key, int value) {
        SharedPreferences pre = getKITSharedPreferences(context);
        pre.edit().putInt(key, value).commit();
    }

    public static int getInt(Context context, String key) {
        SharedPreferences pre = getKITSharedPreferences(context);
        return pre.getInt(key, 0);
    }

    public static void saveImageToGallery(Context context, Bitmap bmp) {
        // 首先保存图片
        File appDir = new File(Environment.getExternalStorageDirectory(), "feng");
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = System.currentTimeMillis() + ".jpg";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 其次把文件插入到系统图库
        try {
            MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        // 最后通知图库更新
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"
                + file.getAbsolutePath() + "/"
                + fileName)));
    }
    public static String saveImageToGallery2(Context context, Bitmap bmp) {
        // 首先保存图片
        File appDir = new File(Environment.getExternalStorageDirectory(), "feng");
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = System.currentTimeMillis() + ".png";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file.getAbsolutePath();
    }

    public static boolean saveSerializable(Context context, String fileName, Serializable data)
    {
        boolean success = false;
        ObjectOutputStream oos = null;
        try
        {
            oos = new ObjectOutputStream(context.openFileOutput(fileName, Context.MODE_PRIVATE));
            oos.writeObject(data);
            success = true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (oos != null)
            {
                try
                {
                    oos.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
        return success;
    }

    public static Serializable readSerialLizable(Context context, String fileName)
    {
        Serializable data = null;
        ObjectInputStream ois = null;
        try
        {
            ois = new ObjectInputStream(context.openFileInput(fileName));
            data = (Serializable) ois.readObject();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (ois != null)
            {
                try
                {
                    ois.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }

        return data;
    }

    public static synchronized String name(Context context,String key){
        String name = getString(context,USER_PHONE_NAME);
        if (TextUtils.isEmpty(name)){
            name = getString(context,USER_PHONE_NUM);
        } else {
        }
        return name;
    }


    public static synchronized String time (){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd");
        Date date = new Date(System.currentTimeMillis());
        return simpleDateFormat.format(date);

    }

//    public static float getMapSize(LatLng latLng, LatLng latLng2){
//        int[] zoom = new int[]{50,100,200,500,1000,2000,5000,10000,20000,25000,50000,100000,200000,500000,1000000,2000000};
//        double distance  =  DistanceUtil. getDistance(latLng2, latLng);
//        for (int i = 0,zoomLen = zoom.length; i < zoomLen; i++) {
//           if(zoom[i] - distance > 0){
//           return 18-i+2.8f;//之所以会多3，是因为地图范围常常是比例尺距离的10倍以上。所以级别会增加3。
//           }
//        };
//       return Api.MAPSIZE;
//    }
}
